import 'package:flutter/material.dart';

import '../../common/common.dart';
import 'my_image.dart';

const String ossStyleStr = "x-oss-process=style/thumbnail";

class MyFace extends StatefulWidget {
  final double size;
  final String? url;
  final VoidCallback? onTap;

  final bool isOssThumbnail;

  MyFace({
    this.url,
    this.size = 60.0,
    this.onTap,
    this.isOssThumbnail: true,
    Key? key,
  }) : super(key: key);

  @override
  _MyFaceState createState() => _MyFaceState();
}

class _MyFaceState extends State<MyFace> {
  String url = "";

  @override
  void initState() {
    if (widget.url != null && widget.url!.isNotEmpty) {
      url = widget.url!;
      if (widget.isOssThumbnail && ossStyleStr.isNotEmpty) {
        if (url.indexOf("?") > 0) {
          url = url + "&" + ossStyleStr;
        } else {
          url = url + "?" + ossStyleStr;
        }
      }
    }

    logd("MyFace", "url:" + url);

    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return InkWell(
      child: ClipRRect(
        borderRadius: BorderRadius.circular(6),
        child: url.isNotEmpty
            ? Image.network(
                url,
                width: widget.size,
                height: widget.size,
              )
            : MyPackageImage("assets/img/face.png",
                width: widget.size, height: widget.size),
      ),
      onTap: widget.onTap,
    );
  }
}
